package com.foreknow.view;

import com.foreknow.bean.Admin;
import com.foreknow.service.AdminService;
import com.foreknow.service.impl.AdminServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Set;

public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setTitle("\u767B\u5F55");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("\u767B\u5F55");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//登录的事件监听
				//获取用户录入的数据
				String username = textField.getText();
				String password = passwordField.getText();
				//调用业务层Service的方法
				AdminService service = new AdminServiceImpl();
				Admin admin = service.isValidate(username,password);
				if(admin != null) {
					System.out.println(admin.getUsername() + "--" + admin.getPassword());
				}else {
					System.out.println("no user......");
				}
			}
		});
		btnNewButton.setBounds(58, 208, 93, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u6CE8\u518C");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//注册
				AdminService service = new AdminServiceImpl();
				Admin admin = new Admin();
				admin.setUsername(textField.getText());
				admin.setPassword(passwordField.getText());
				boolean isRight = service.save(admin);
				System.out.println(isRight);
			}
		});
		btnNewButton_1.setBounds(171, 208, 93, 23);
		contentPane.add(btnNewButton_1);
		
		JLabel label = new JLabel("\u7528\u6237\u540D: ");
		label.setBounds(87, 65, 54, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("\u5BC6\u7801\uFF1A");
		label_1.setBounds(87, 122, 54, 15);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(146, 62, 206, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(145, 119, 207, 21);
		contentPane.add(passwordField);
		
		JButton button = new JButton("\u53D6\u6D88");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});
		button.setBounds(286, 208, 93, 23);
		contentPane.add(button);
	}

	public int sNumber(int[] nums) {
		Set<Integer> set = new HashSet<>();
		for(int i = 0;i<nums.length;i++) {
			if(set.contains(nums[i])) {
				set.remove(nums[i]);
			}else {
				set.add(nums[i]);
			}
		}

		for(int i = 0;i<nums.length;i++) {
			if(set.contains(nums[i])) {
				return nums[i];
			}
		}
		return -1;
	}
}
